﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="SortedList.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Collections.Generic;

#endregion

namespace IveelySE.NLP.Common.DataStructure.List
{
    /// <summary>
    ///   (按照数的大小)排序泛型列表
    /// </summary>
    /// <typeparam name="T"> 类型 </typeparam>
    [Serializable]
    public class SortedList<T> : List<T>
    {
        #region 公有方法

        public SortedList(IEnumerable<T> collection)
        {
            foreach(T item in collection)
            {
                this.Add(item);
            }
        }

        public SortedList()
        {
        }

        /// <summary>
        ///   插入
        /// </summary>
        /// <param name="item"> </param>
        public new void Add(T item)
        {
            try
            {
                int position = this.BinarySearch(item);
                position = position > 0 ? position : ~position;
                this.Insert(position, item);
            }
            catch
            {
                base.Add(item);
            }
        }

        public void Modify(T item, int index)
        {
            this.RemoveAt(index);
            this.Add(item);
        }

        #endregion
    }
}